#include "stdafx.h"
#include "CSquare.h"


CSquare::CSquare(void)
{
}

CSquare::CSquare(CPoint pBegin, CPoint pEnd, COLORREF cPen)
{
	this->m_pBegin	= pBegin;
	this->m_pEnd	= pEnd;
	m_nAngle = NUM_OF_ANGLE;
	m_pCoordinates = new CPoint[NUM_OF_ANGLE];

	this->m_cPenColor = cPen;
	m_brushColor.CreateSolidBrush(RGB(255, 255, 255));
	m_nPenStyle = PS_SOLID;
	m_nPenWidth = 2;
}

CSquare::CSquare (CPoint m_pBegin, CPoint m_pEnd, COLORREF m_clrBack, COLORREF m_cPenColor, BASESHAPE__T m_nShapeType, INT m_nRotate)
{
	this->m_nRotate = m_nRotate;
	this->m_pBegin = m_pBegin;
	this->m_pEnd = m_pEnd;
	this->m_clrBack = m_clrBack;
	this->m_cPenColor = m_cPenColor;
	this->m_nShapeType = m_nShapeType;
	m_brushColor.CreateSolidBrush(RGB(255,255,255));
//	this->m_szText = m_szText;
//	this->m_color = m_color;
	m_nAngle = NUM_OF_ANGLE;
	m_pCoordinates = new CPoint[NUM_OF_ANGLE];
	m_nPenStyle = PS_SOLID;
	m_nPenWidth = 2;
}

void CSquare::Serialize (CArchive &ar)
{
	if (ar.IsStoring() )
	{
		ar << m_pBegin << m_pEnd << m_clrBack << m_cPenColor << m_nShapeType << m_szText << m_color << m_nRotate;
	}
	else
	{
		int nShapeType;
		ar >> m_pBegin >> m_pEnd >> m_clrBack >> m_cPenColor >> nShapeType >> m_szText >> m_color >> m_nRotate;
		m_nShapeType = (BASESHAPE__T)nShapeType;
	}
}

BOOL CSquare::Draw(CDC *cDC)
{
	CPen cPen(m_nPenStyle, m_nPenWidth, m_cPenColor);
	CPen *pOldPen = cDC->SelectObject(&cPen);
	CBrush brushColor (m_clrBack);
	CBrush *OldBrush = cDC->SelectObject(&brushColor);

	this->setCoordinate();
	cDC->Polygon(m_pCoordinates, m_nAngle);

	cDC->SelectObject(pOldPen);
	cPen.DeleteObject();
	cDC->SelectObject (OldBrush);
	brushColor.DeleteObject();
	return	FALSE;
}

void CSquare::setCoordinate()
{
	CPoint Center;
	radiansOffset = DegreesToRadians(AngleDist() + m_nRotate);
	m_pCoordinates[0] = m_pBegin;
	lengthSide = (INT)sqrt((m_pBegin.x - m_pEnd.x) * (m_pBegin.x - m_pEnd.x) + (m_pBegin.y - m_pEnd.y) * (m_pBegin.y - m_pEnd.y)) / 2;
	Center.x = m_pBegin.x - lengthSide * cos(radiansOffset + 0 * 2 * PI / NUM_OF_ANGLE);
	Center.y = m_pBegin.y - lengthSide * sin(radiansOffset + 0 * 2 * PI / NUM_OF_ANGLE);
	for( int i=1; i < NUM_OF_ANGLE; ++i )
	{
		const double angle = radiansOffset + i * 2 * PI / NUM_OF_ANGLE;
		m_pCoordinates[i].x = Center.x + lengthSide * cos(angle);
		m_pCoordinates[i].y = Center.y + lengthSide * sin(angle);
	}
	m_pMax = m_pMin = m_pBegin;
	for(int i = 0; i < m_nAngle; i++)
	{
		m_pMin.x = min(m_pMin.x, m_pCoordinates[i].x);
		m_pMin.y = min(m_pMin.y, m_pCoordinates[i].y);
		m_pMax.x = max(m_pMax.x, m_pCoordinates[i].x);
		m_pMax.y = max(m_pMax.y, m_pCoordinates[i].y);

	}
}


CSquare::~CSquare(void)
{
}

int CSquare::AngleDist()
{
	if( ( m_pEnd.x < m_pBegin.x ) && ( m_pEnd.y < m_pBegin.y ) )
		return 45;
	else if(( m_pEnd.x > m_pBegin.x ) && ( m_pEnd.y < m_pBegin.y ))
		return 135;
	else if((m_pEnd.x > m_pBegin.x ) && ( m_pEnd.y > m_pBegin.y ))
		return 225;
	else return 315;
}